<?php
  require_once("db.php");
  $msg = '';

//echo '<pre>';
//print_r($_COOKIE);

  if(isset($_SESSION['id']) || !empty($_SESSION['id'])) {
    header('location:'.SITE_PATH.'/display_users/index.php');
    exit;
  }


if(isset($_POST['email']) &&
  !empty($_POST['email']) &&
  isset($_POST['password']) &&
  !empty($_POST['password']))
  {
    pr($_POST);

    $_POST = array_map('trim',$_POST);
    $_POST = array_map('mysql_real_escape_string',$_POST);

    $email = $_POST['email'];
    $password = md5($_POST['password']);

    $query = "SELECT * FROM `users`
    WHERE `email`='$email' AND
          `password`='$password' AND
          `status`='1'";
    $result = mysql_query($query) or die(mysql_error());
      pq($query);
    if(mysql_num_rows($result) == 1) {


//start set cookie
  $time = isset($_POST['remember'])?(time()+30):(time()-1);
  setcookie('email',$_POST['email'],$time);
  setcookie('password',$_POST['password'],$time);
//end set cookie

      $user = mysql_fetch_assoc($result);
      $_SESSION['id'] = $user['id'];
      $_SESSION['uname'] = $user['uname'];
      $_SESSION['email'] = $user['email'];
      header('location:display_users/index.php');

        pr($user);
    } else {
      $msg = "Invalid Email / Password.";
    }
  } else {
    $msg = "Email / Password required.";
  }

  if($msg != '') {
    echo "<h4>$msg</h4>";
  }
?>
<form action="" method="post">
  <table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
    <thead>
      <tr>
        <th colspan="2">::Login Here::</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td align="right">Email :</td>
        <td ><input type="text" value="<?=@$_COOKIE['email']?>" name="email" /></td>
      </tr>
      <tr>
        <td align="right">Password :</td>
        <td ><input value="<?=@$_COOKIE['password']?>" type="password" name="password" /></td>
      </tr>

      <tr>
        <td align="right">&nbsp;</td>
        <td >
          <label><input checked type="checkbox" name="remember" />Remember me for 1 minute</label></td>
      </tr>

      <tr>
        <td align="right">&nbsp;</td>
        <td ><input type="submit" name="submit" value="login"/></td>
      </tr>
    </tbody>
  </table>
</form>

<style type="text/css">
  input[type="text"],
  input[type="password"]{
    background-color:yellow;
  }
</style>
<?php
  require_once("debug.php");
?>
